package com.qire.manhua.controller;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.qire.common.support.base.EventController;
import com.qire.manhua.activity.helper.LoginRouter;
import com.qire.manhua.databinding.FragmentBookshelfBinding;
import com.qire.manhua.fragment.BookshelfFragment;
import com.qire.manhua.model.viewModel.impl.BookshelfViewModel;

import androidx.annotation.NonNull;

public class BookshelfController extends EventController<BookshelfFragment> {

    private BookshelfViewModel viewModel;

    public BookshelfController(@NonNull BookshelfFragment owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        FragmentBookshelfBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setBookshelfController(this);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(BookshelfViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**********************************************************************************************/

    public void selectAllBtnOnClick(View view) {
        viewModel.toggleCheckAll();
    }

    public void editBtnOnClick(View view) {
        if(!LoginRouter.ROUTER.checkLoginRouteIfNeed()) {
            viewModel.toggleEditMode();
        }
    }

    public void exitEditBtnOnClick(View view) {
        viewModel.toggleEditMode();
    }

    public void viewPagerOnPageSelected(int position) {
        viewModel.setCurrentPageNo(position);
        SmartTabLayout bookshelfTabLayout = owner.viewDataBinder().bookshelfTabLayout;
        for(int index = 0; index < 3; index++) {
            TextView tabText = (TextView) bookshelfTabLayout.getTabAt(index);
            if (tabText != null) {
                tabText.setTextSize(index == position ? 20 : 15);
                tabText.setTypeface(index == position ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
            }
        }
    }

}
